using System;
using System.Collections.Generic;
using UnityEngine;
using QFramework;
using Unity.VisualScripting;
using UnityEngine.Rendering.Universal;
using Random = UnityEngine.Random;

namespace ProjectSurvival
{
    public partial class SimpleRotateSword : ViewController
    {
        private float rotationSpeed;
        private float angle = 0f;
        [SerializeField] private bool isInvert = false;
        //private CircleCollider2D _circleCollider2D;

        private List<Collider2D> _swords = new List<Collider2D>();
        void CreatSword()
        {
            _swords.Add(RotateSword.InstantiateWithParent(this)
                .Self((self) =>
                {
                    var selfCatch = self;
                    selfCatch.OnTriggerEnter2DEvent((other) =>
                    {
                        var hurtBox = other.GetComponent<HitHurtBox>();
                        if (hurtBox)
                        {
                            if (hurtBox.Owner.CompareTag("Enemy"))
                            {
                                //hurtBox.Owner.Hurt(Global.SimpleRotateSwordDamage.Value);
                                var damageTime = Global.SuperRotateSword.Value ? Random.Range(2, 3) + 1 : 1;
                                DamageSystem.CalculateDamage(Global.SimpleRotateSwordDamage.Value * damageTime,
                                    hurtBox.Owner.GetComponent<Enemy>());
                                if (Random.Range(0, 1f) < 0.5f)
                                {
                                    other.attachedRigidbody.velocity =
                                        other.NormalizedDirection2DFrom(selfCatch) * 5 +
                                        other.NormalizedDirection2DFrom(Player.Instance) * 10;
                                }
                            }
                        }
                    }).UnRegisterWhenGameObjectDestroyed(selfCatch);
                })
                .Show()
            );
            SetSwordPos();
        }

        void CreatSwords(int creatCount)
        {
            for (int i = 0; i < creatCount; i++)
            {
                CreatSword();
            }
        }

        void Start()
        {
            RotateSword.Hide();
            Global.SimpleRotateSwordCount.Or(Global.AdditionalFlyThingCount).Register(() =>
            {
                int creatCount = Global.SimpleRotateSwordCount.Value + Global.AdditionalFlyThingCount.Value -
                                 _swords.Count;
                if (creatCount > 0)
                {
                    CreatSwords(creatCount);
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            Global.SimpleRotateSwordRange.Register((range) => { SetSwordPos(); })
                .UnRegisterWhenGameObjectDestroyed(gameObject);
            Global.SimpleRotateSwordSpeed.RegisterWithInitValue(speed => { rotationSpeed = speed; })
                .UnRegisterWhenGameObjectDestroyed(gameObject);
            CreatSword();
        }

        private void SetSwordPos()
        {
            var radius = Global.SimpleRotateSwordRange.Value;
            var durationDegrees = 360 / _swords.Count;
            int i = 0;
            foreach (var rotateSword in _swords)
            {
                float x = Mathf.Cos(durationDegrees * i * Mathf.Deg2Rad) * radius;
                float y = Mathf.Sin(durationDegrees * i * Mathf.Deg2Rad) * radius;
                rotateSword.transform.localPosition = new Vector3(x, y);
                rotateSword.LocalEulerAnglesZ(durationDegrees * i - 90);
                i++;
            }
            // for (int i = 0; i < _swords.Count; i++)
            // {
            //     var circleLocalPos = new Vector2(Mathf.Cos(durationDegrees * i * Mathf.Deg2Rad),
            //         Mathf.Sin(durationDegrees * i * Mathf.Deg2Rad)) * radius;
            //     _swords[i].LocalPosition(circleLocalPos.x, circleLocalPos.y)
            //         .LocalEulerAnglesZ(durationDegrees * i - 90);
            // }
        }

        void Update()
        {
            //激活超级武器模式后的数值调整
            var speedTime = Global.SuperRotateSword.Value ? 10 : 1;
            if (isInvert)
            {
                rotationSpeed = -rotationSpeed;
                isInvert = false;
            }

            angle += rotationSpeed * Time.deltaTime * 20*speedTime;
            this.LocalEulerAnglesZ(-angle);
        }
    }
}